<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use DateTimeInterface;

class Crm_Wall_Zan extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasFactory;

    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'crm_wall_zan';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'agencyid',
        'wid',
        'mid',
        'is_zan'
    ];



    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function hasOneMember()
    {
        return $this->hasOne('App\Models\Crm_Member', 'id', 'mid');
    }

    public function hasOneDynamic()
    {
        return $this->hasOne('App\Models\Crm_Dynamic', 'id', 'dyid');
    }

    public static function getFilter($filter)
    {
        return self::whereHas('hasOneMember',function ($q) use ($filter){
            if (isset($filter['username']) && !empty($filter['username'])) {
                $q->where('username', 'LIKE', '%'.$filter['username'].'%');
            }

        })->where(function ($query) use ($filter){
            if (isset($filter['mid']) && !empty($filter['mid'])) {
                $query->where('mid', $filter['mid']);
            }

            $query->where('agencyid', $filter['aid'])->where('is_zan','Y');
        });
    }
}
